package com.hotel.BL;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.EntityName;
import com.StatusTitle;
import com.google.appengine.api.datastore.Entity;
import com.google.appengine.api.datastore.EntityNotFoundException;
import com.google.appengine.api.datastore.FetchOptions;
import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;
import com.google.appengine.api.datastore.Query;
import com.google.appengine.api.datastore.Query.CompositeFilterOperator;
import com.google.appengine.api.datastore.Query.Filter;
import com.google.appengine.api.datastore.Query.FilterOperator;
import com.google.appengine.api.datastore.Query.FilterPredicate;

import data.CurrencyMaster;
import data.POSTax;
import data.Tax;

public class POSTaxLogic extends AbstractBL {
	public POSTaxLogic(HttpServletRequest req,HttpServletResponse resp){
		this.req = req;
		this.resp = resp;
	}
	public List<POSTax> GeneratePOSTax(long hotelId){
		List<POSTax> list = new ArrayList<POSTax>();
		MasterCreator master = new MasterCreator();
		CurrencyMaster thb = master.GetCurrency(StatusTitle.CurrencyTitle.THB);
		
		POSTax e;
		if(GetPOSTax(hotelId,"Vat") == null){
			e = new POSTax();
			e.setHotelId(hotelId);
			e.setTitle("Vat");
			e.setCurrencyValue(7.0);
			e.setCurrency(thb);
			e.setPercent(true);
			e.setSortOrder(0);
			list.add(e);
		}
		if(GetPOSTax(hotelId,"Service Charge") == null){
			e = new POSTax();
			e.setHotelId(hotelId);
			e.setTitle("Service Charge");
			e.setCurrencyValue(50.0);
			e.setCurrency(thb);
			e.setPercent(false);
			e.setSortOrder(0);
			list.add(e);
		}
		return list;
	}
	
	public POSTax GetPOSTax(long id)
	{
		Key key = KeyFactory.createKey(EntityName.POSTax, id);
		try {
			Entity lang = datastore.get(key);
			POSTax c = new POSTax();
			c.setEntity(lang);
			return c;
		} catch (EntityNotFoundException e) {
			return null;
		}
	}
	
	public POSTax GetPOSTax(long hotelId,String title)
	{
		Query q = new Query(EntityName.POSTax);
		
		Filter filterHotel = new FilterPredicate("hotelId",FilterOperator.EQUAL,hotelId);
		Filter filterStatus = new FilterPredicate("status",FilterOperator.EQUAL,true);
		Filter filterAvaliable = new FilterPredicate("avaliable",FilterOperator.EQUAL,true);
		Filter filterTitle = new FilterPredicate("title",FilterOperator.EQUAL,title);

		Filter filter = CompositeFilterOperator.and(filterHotel, filterStatus, filterAvaliable,filterTitle);
		
		q.setFilter(filter);
		
		return GetPOSTax(datastore.prepare(q).asSingleEntity());
	}
	
	public List<POSTax> GetAllPOSTax(long hotelId)
	{
		Query q = new Query(EntityName.POSTax);
		
		Filter filterHotel = new FilterPredicate("hotelId",FilterOperator.EQUAL,hotelId);
		Filter filterAvaliable = new FilterPredicate("avaliable",FilterOperator.EQUAL,true);

		Filter filter = CompositeFilterOperator.and(filterHotel, filterAvaliable);;
		
		q.setFilter(filter);
		
		return GetPOSTaxList(datastore.prepare(q).asList(FetchOptions.Builder.withDefaults()));
	}
	
	public POSTax GetPOSTax(Entity entity)
	{
		if(entity == null) return null;
		POSTax item = new POSTax();
		item.setEntity(entity);
		return item;
	}
	
	public List<POSTax> GetPOSTaxList(List<Entity> entities)
	{
		List<POSTax> list = new ArrayList<POSTax>();
		for(Entity e : entities)
		{
			POSTax a = new POSTax();
			a.setEntity(e);
			list.add(a);
		}
		return list;
	}
	
	public List<Tax> GetList(List<Entity> entities)
	{
		List<Tax> list = new ArrayList<Tax>();
		for(Entity e : entities)
		{
			Tax a = new Tax();
			a.setEntity(e);
			list.add(a);
		}
		return list;
	}
	
	public List<POSTax> GetAllPOSTaxLimit(long hotelId,long offset,long line)
	{
		Query q = new Query(EntityName.POSTax);
		
		Filter filterHotel = new FilterPredicate("hotelId",FilterOperator.EQUAL,hotelId);
		Filter filterAvaliable = new FilterPredicate("avaliable",FilterOperator.EQUAL,true);

		Filter filter = CompositeFilterOperator.and(filterHotel, filterAvaliable);
		q.setFilter(filter);
		return GetPOSTaxList(datastore.prepare(q).asList(FetchOptions.Builder.withLimit((int) line).offset((int)offset)));
		//return GetPOSUnitList(datastore.prepare(q).asList(FetchOptions.Builder.withDefaults()));
	}
	
	public POSTax AddPOSTax(long id,String title,String description,String chargeType,Double amount) throws EntityNotFoundException{
		POSTax a = new POSTax();
		boolean isPercent = true;
		a.setHotelId(id);
		a.setTitle(title);
		a.setDescription(description);
		a.setCurrencyValue(amount);
		if(chargeType.equals("true"))
			isPercent = true;
		else
			isPercent = false;
		a.setPercent(isPercent);
		a.setAvaliable(true);
		a.setStatus(true);
		Key k = datastore.put(a.getEntity());
		a.setId(k.getId());
		a.setKey(k);
		return a;
	}
	
	public POSTax EditPOSTax(long UnitId,String title,String des,String chargeType, Double amount) throws EntityNotFoundException{
		POSTax a = GetPOSTax(UnitId);
		a.setTitle(title);
		a.setDescription(des);
		boolean isPercent = true;
		if(chargeType.equals("true"))
			isPercent = true;
		else
			isPercent = false;
		a.setPercent(isPercent);
		a.setCurrencyValue(amount);
		Key k = datastore.put(a.getEntity());
		a.setId(k.getId());
		a.setKey(k);

		return a;
	}
	
	public POSTax ChangeStatus(long taxId) throws EntityNotFoundException{
		POSTax a = GetPOSTax(taxId);
		if(a.isStatus() == true){
			a.setStatus(false);
		}else if(a.isStatus() == false){
			a.setStatus(true);
		}
		 
		Key k = datastore.put(a.getEntity());
		a.setId(k.getId());
		a.setKey(k);

		return a;
	}
	
	public POSTax EditSortOrder(long id,long sortOrder) throws EntityNotFoundException{
		POSTax a = GetPOSTax(id);
		a.setSortOrder(sortOrder);
		Key k = datastore.put(a.getEntity());
		a.setId(k.getId());
		a.setKey(k);
		return a;
	}
	
	public POSTax DeletePOSTax(long taxId) throws EntityNotFoundException{
		POSTax a = GetPOSTax(taxId);
		a.setAvaliable(false);
		a.setStatus(false);
		Key k = datastore.put(a.getEntity());
		a.setId(k.getId());
		a.setKey(k);
		return a;
	}
}
